using System.ComponentModel;
using System.Data;
using MySqlConnector;

namespace Front._1118;

public partial class BookForm : Form
{
    public BookForm()
    {
        InitializeComponent();
    }

    private void BookForm_Load(object sender, EventArgs e)
    {
        
        GetAllBooks();
        
        
        GetAllCategories();
        
    }

    private void GetAllCategories()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "SELECT DISTINCT book_category FROM book_info";
        // 创建-对象
        MySqlDataAdapter  mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建dataset
        DataSet  dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_category");
        if (dataSet.Tables["book_category"].Rows.Count > 0)
        {
            DataTable categoryTable = dataSet.Tables["book_category"];
            // 插入一行全部
            DataRow row = categoryTable.NewRow();
            row[0]  = "全部";
    
            categoryTable.Rows.InsertAt(row,0);
            comboBox1.DataSource = categoryTable;
            comboBox1.DisplayMember = categoryTable.Columns[0].ColumnName;
        }
    }

    private void GetAllBooks()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        String sql = "SELECT * FROM book_info";
        // 创建-对象
        MySqlDataAdapter  mySqlDataAdapter = new MySqlDataAdapter(sql, connection);
        //创建dataset
        DataSet  dataSet = new DataSet();
        //填充数据
        mySqlDataAdapter.Fill(dataSet, "book_info");
        this.dataGridView1.DataSource = dataSet.Tables["book_info"];
    }


    private void button1_Click(object sender, EventArgs e)
    {
        AddBookF addBookF = new AddBookF();
        DialogResult result = addBookF.ShowDialog();
        if (result == DialogResult.OK)
        {
            //刷新
            GetAllBooks();
        }
        else
        {
            
        }
            
    }

    private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //获取选中的行
        DataGridViewRow row = this.dataGridView1.SelectedRows[0];
        int bookId = (int)row.Cells[0].Value;
        DialogResult result = MessageBox.Show("确定删除此条数据么？","警告",MessageBoxButtons.OKCancel);
        if (result == DialogResult.OK)
        {
            //删除
            using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
            connection.Open();
            MessageBox.Show("数据库连接成功！");
            String sql = "delete from book_info where book_id=@book_id;";
            using var command = new MySqlCommand(sql, connection);
            //给sql变量提供值
            command.Parameters.AddWithValue("book_id", bookId);
            int count = command.ExecuteNonQuery();
            if (count > 0)
            {
                MessageBox.Show("删除成功");
                GetAllBooks();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            
        }
        else
        {
            
        }
    }

    
}